package com.android.basic.ktExtension.flyn;


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import java.lang.reflect.Field;


@TargetApi(Build.VERSION_CODES.KITKAT)
class EyesKitKat {

  private static final String TAG_FAKE_STATUS_BAR_VIEW = "statusBarView";
  private static final String TAG_MARGIN_ADDED = "marginAdded";

  static void setStatusBarColor(Activity activity, int statusColor) {
    Window window = activity.getWindow();
    //设置Window为全透明
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);
    //获取父布局
    View mContentChild = mContentView.getChildAt(0);
    //获取状态栏高度
    int statusBarHeight = getStatusBarHeight(activity);

    //如果已经存在假状态栏则移除，防止重复添加
    removeFakeStatusBarViewIfExist(activity);
    //添加一个View来作为状态栏的填充
    addFakeStatusBarView(activity, statusColor, statusBarHeight);
    //设置子控件到状态栏的间距
    addMarginTopToContentChild(mContentChild, statusBarHeight);
    //不预留系统栏位置
    if (mContentChild != null) {
      mContentChild.setFitsSystemWindows(false);
    }
    //如果在Activity中使用了ActionBar则需要再将布局与状态栏的高度跳高一个ActionBar的高度，否则内容会被ActionBar遮挡
    int action_bar_id = activity.getResources()
        .getIdentifier("action_bar", "id", activity.getPackageName());
    View view = activity.findViewById(action_bar_id);
    if (view != null) {
      TypedValue typedValue = new TypedValue();
      if (activity.getTheme().resolveAttribute(R.attr.actionBarSize, typedValue, true)) {
        int actionBarHeight = TypedValue.complexToDimensionPixelSize(typedValue.data,
            activity.getResources().getDisplayMetrics());
        Eyes.setContentTopPadding(activity, actionBarHeight);
      }
    }
  }

  static void translucentStatusBar(Activity activity) {
    Window window = activity.getWindow();
    //设置Window为透明
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    ViewGroup mContentView = activity.findViewById(Window.ID_ANDROID_CONTENT);
    View mContentChild = mContentView.getChildAt(0);

    //移除已经存在假状态栏则,并且取消它的Margin间距
    removeFakeStatusBarViewIfExist(activity);
    removeMarginTopOfContentChild(mContentChild, getStatusBarHeight(activity));
    if (mContentChild != null) {
      //fitsSystemWindow 为 false, 不预留系统栏位置.
      mContentChild.setFitsSystemWindows(false);
    }
  }

  static void setStatusBarColorForCollapsingToolbar(final Activity activity,
      final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout,
      Toolbar toolbar, int statusColor) {
    Window window = activity.getWindow();
    //设置Window为全透明
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);

    //AppBarLayout,CollapsingToolbarLayout,ToolBar,ImageView的fitsSystemWindow统一改为false, 不预留系统栏位置.
    View mContentChild = mContentView.getChildAt(0);
    mContentChild.setFitsSystemWindows(false);
    ((View) appBarLayout.getParent()).setFitsSystemWindows(false);
    appBarLayout.setFitsSystemWindows(false);
    collapsingToolbarLayout.setFitsSystemWindows(false);
    collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false);

    toolbar.setFitsSystemWindows(false);
    //为Toolbar添加一个状态栏的高度, 同时为Toolbar添加paddingTop,使Toolbar覆盖状态栏，ToolBar的title可以正常显示.
    if (toolbar.getTag() == null) {
      CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar
          .getLayoutParams();
      int statusBarHeight = getStatusBarHeight(activity);
      lp.height += statusBarHeight;
      toolbar.setLayoutParams(lp);
      toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight,
          toolbar.getPaddingRight(), toolbar.getPaddingBottom());
      toolbar.setTag(true);
    }
    //移除已经存在假状态栏则,并且取消它的Margin间距
    int statusBarHeight = getStatusBarHeight(activity);
    removeFakeStatusBarViewIfExist(activity);
    removeMarginTopOfContentChild(mContentChild, statusBarHeight);
    //添加一个View来作为状态栏的填充
    final View statusView = addFakeStatusBarView(activity, statusColor, statusBarHeight);

    CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout
        .getLayoutParams()).getBehavior();
    if (behavior != null && behavior instanceof AppBarLayout.Behavior) {
      int verticalOffset = ((AppBarLayout.Behavior) behavior).getTopAndBottomOffset();
      if (Math.abs(verticalOffset) > appBarLayout.getHeight() - collapsingToolbarLayout
          .getScrimVisibleHeightTrigger()) {
        statusView.setAlpha(1f);
      } else {
        statusView.setAlpha(0f);
      }
    } else {
      statusView.setAlpha(0f);
    }
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
      @Override
      public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (Math.abs(verticalOffset) > appBarLayout.getHeight() - collapsingToolbarLayout
            .getScrimVisibleHeightTrigger()) {
          //toolbar被折叠时显示状态栏
          if (statusView.getAlpha() == 0) {
            statusView.animate().cancel();
            statusView.animate().alpha(1f)
                .setDuration(collapsingToolbarLayout.getScrimAnimationDuration()).start();
          }
        } else {
          //toolbar展开时显示状态栏
          if (statusView.getAlpha() == 1) {
            statusView.animate().cancel();
            statusView.animate().alpha(0f)
                .setDuration(collapsingToolbarLayout.getScrimAnimationDuration()).start();
          }
        }
      }
    });
  }

  static void setStatusBarWhiteForCollapsingToolbar(final Activity activity,
      AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout,
      Toolbar toolbar, final int statusBarColor) {
    final Window window = activity.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT);
    View mContentChild = mContentView.getChildAt(0);
    mContentChild.setFitsSystemWindows(false);
    ((View) appBarLayout.getParent()).setFitsSystemWindows(false);
    appBarLayout.setFitsSystemWindows(false);
    collapsingToolbarLayout.setFitsSystemWindows(false);
    collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false);
    toolbar.setFitsSystemWindows(false);

    if (toolbar.getTag() == null) {
      CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar
          .getLayoutParams();
      int statusBarHeight = getStatusBarHeight(activity);
      lp.height += statusBarHeight;
      toolbar.setLayoutParams(lp);
      toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight,
          toolbar.getPaddingRight(), toolbar.getPaddingBottom());
      toolbar.setTag(true);
    }

    int statusBarHeight = getStatusBarHeight(activity);
    int color = Color.BLACK;
    try {
      Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
      color = statusBarColor;
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    try {
      Field darkFlag = WindowManager.LayoutParams.class
          .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
      color = statusBarColor;
    } catch (NoSuchFieldException e) {
      e.printStackTrace();
    }
    final View statusView = addFakeStatusBarView(activity, color, statusBarHeight);
    CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout
        .getLayoutParams()).getBehavior();
    if (behavior != null && behavior instanceof AppBarLayout.Behavior) {
      int verticalOffset = ((AppBarLayout.Behavior) behavior).getTopAndBottomOffset();
      if (Math.abs(verticalOffset) > appBarLayout.getHeight() - collapsingToolbarLayout
          .getScrimVisibleHeightTrigger()) {
        statusView.setAlpha(1f);
      } else {
        statusView.setAlpha(0f);
      }
    } else {
      statusView.setAlpha(0f);
    }

    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
      private final static int EXPANDED = 0;
      private final static int COLLAPSED = 1;
      private int appBarLayoutState;

      @Override
      public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (Math.abs(verticalOffset) >= (appBarLayout.getTotalScrollRange() - Eyes
            .getPxFromDp(activity, 56))) {
          if (appBarLayoutState != COLLAPSED) {
            appBarLayoutState = COLLAPSED;

            if (Eyes.MIUISetStatusBarLightMode(activity, true) || Eyes
                .FlymeSetStatusBarLightMode(activity, true)) {
            }
            if (statusView.getAlpha() == 0) {
              statusView.animate().cancel();
              statusView.animate().alpha(1f)
                  .setDuration(collapsingToolbarLayout.getScrimAnimationDuration()).start();
            }
          }
        } else {
          if (appBarLayoutState != EXPANDED) {
            appBarLayoutState = EXPANDED;

            if (Eyes.MIUISetStatusBarLightMode(activity, false) || Eyes
                .FlymeSetStatusBarLightMode(activity, false)) {
            }
            if (statusView.getAlpha() == 1) {
              statusView.animate().cancel();
              statusView.animate().alpha(0f)
                  .setDuration(collapsingToolbarLayout.getScrimAnimationDuration()).start();
            }
            translucentStatusBar(activity);
          }
        }
      }
    });
  }

  private static void removeFakeStatusBarViewIfExist(Activity activity) {
    Window window = activity.getWindow();
    ViewGroup mDecorView = (ViewGroup) window.getDecorView();

    View fakeView = mDecorView.findViewWithTag(TAG_FAKE_STATUS_BAR_VIEW);
    if (fakeView != null) {
      mDecorView.removeView(fakeView);
    }
  }

  private static View addFakeStatusBarView(Activity activity, int statusBarColor,
      int statusBarHeight) {
    Window window = activity.getWindow();
    ViewGroup mDecorView = (ViewGroup) window.getDecorView();

    View mStatusBarView = new View(activity);
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
    layoutParams.gravity = Gravity.TOP;
    mStatusBarView.setLayoutParams(layoutParams);
    mStatusBarView.setBackgroundColor(statusBarColor);
    mStatusBarView.setTag(TAG_FAKE_STATUS_BAR_VIEW);

    mDecorView.addView(mStatusBarView);
    return mStatusBarView;
  }

  private static void addMarginTopToContentChild(View mContentChild, int statusBarHeight) {
    if (mContentChild == null) {
      return;
    }
    if (!TAG_MARGIN_ADDED.equals(mContentChild.getTag())) {
      FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentChild.getLayoutParams();
      lp.topMargin += statusBarHeight;
      mContentChild.setLayoutParams(lp);
      mContentChild.setTag(TAG_MARGIN_ADDED);
    }
  }

  private static int getStatusBarHeight(Context context) {
    int result = 0;
    int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resId > 0) {
      result = context.getResources().getDimensionPixelOffset(resId);
    }
    return result;
  }

  private static void removeMarginTopOfContentChild(View mContentChild, int statusBarHeight) {
    if (mContentChild == null) {
      return;
    }
    if (TAG_MARGIN_ADDED.equals(mContentChild.getTag())) {
      FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentChild.getLayoutParams();
      lp.topMargin -= statusBarHeight;
      mContentChild.setLayoutParams(lp);
      mContentChild.setTag(null);
    }
  }


}
